package com.stox.ui.javafx.core.event.handler;

import javafx.application.Platform;
import javafx.geometry.Bounds;
import javafx.scene.Node;

import com.stox.ui.javafx.core.widget.StoxStage;

public class BoundsInParentPropertyChangeListener extends AbstractPropertyChangeListener<Bounds> {
	
	public BoundsInParentPropertyChangeListener(Node node) {
		super(node);
	}

	@Override
	public void changed(final Bounds newValue, final Node node) {
			Platform.runLater(new Runnable(){
				@Override
				public void run() {
					Bounds paneBounds = StoxStage.getInstance().getContentPane().getBoundsInParent();
					if(newValue.getMinX() < paneBounds.getMinX())
						node.setLayoutX(paneBounds.getMinX());
					if(newValue.getMaxX() > paneBounds.getMaxX())
						node.setLayoutX(paneBounds.getMaxX() - newValue.getWidth());
					if(newValue.getMinY() < paneBounds.getMinY())
						node.setLayoutY(paneBounds.getMinY());
					if(newValue.getMaxY() > paneBounds.getMaxY())
						node.setLayoutY(paneBounds.getMaxY() - newValue.getHeight());
					if(null != node.getParent())
						node.getParent().layout();
				}
			});
	}
}
